﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace 通知线程管理器.组合
{
    public delegate void CompleteEvent(string result);
    public delegate void ExceptionEvent(Exception ex);
    public partial class 组合线程管理器 : Form
    {
        NotifyThreadManager f = null;
        public 组合线程管理器()
        {
            InitializeComponent();
        }

        void FactotrComplte(string result)
        {
            try
            {
                return;
            }
            finally
            {
                int i = 0;
            }

            MessageBox.Show(result);
        }

        void FactotrException(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            f = new NotifyThreadManager();
            f.ExecAsync(this, new CompleteEvent(FactotrComplte), new ExceptionEvent(FactotrException));
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            switch (f.WaitForCompleted(1000))
            {
                case AsyncReturn.unknow:
                    MessageBox.Show("unknow");
                    break;
                case AsyncReturn.running:
                    MessageBox.Show("running");
                    break;
                case AsyncReturn.completeUnSuccessfully:
                    MessageBox.Show("completeUnSuccessfully");
                    break;
                case AsyncReturn.completeSuccessfully:
                    MessageBox.Show("completeSuccessfully");
                    break;
                default:
                    break;
            }
        }
    }


    public class NotifyThreadManager
    {
        Control _con;
        CompleteEvent _com;
        ExceptionEvent _ex;

        Thread th = null;
        public void ExecAsync(Control con, CompleteEvent com, ExceptionEvent ex)
        {
            _con = con;
            _com = com;
            _ex = ex;

            ThreadStart threadStart = new ThreadStart(DoExecution);
            th = new Thread(threadStart);
            th.Start();
        }

        bool mbErr = false;
        public AsyncReturn WaitForCompleted(int waittime)
        {
            if ((th.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted)) == 0)
            {
                if (!th.Join(waittime))
                {
                    return AsyncReturn.running;
                }
            }

            if (mbErr)
                return AsyncReturn.completeUnSuccessfully;
            return AsyncReturn.completeSuccessfully;
        }

        void DoExecution()
        {
            object[] paraArray = new object[1];
            try
            {
                DoShushu();//耗时操作
                paraArray[0] = "数数完毕！";
                _con.BeginInvoke(_com, paraArray);
            }
            catch (Exception e)
            {
                paraArray[0] = e;
                _con.BeginInvoke(_ex, paraArray);
            }
        }

        private void DoShushu()
        {
            int i = 0;
            while (i < 15)
            {

                Thread.Sleep(1000);
                i++;
            }
        }
    }
}